Imports System.Math

Public Class ComputeAbsoluteMeasures

    'Adapted from Alsop DC, Detre JA. Reduced transit time sensitivity in non-invasive magnetic
    'resonance imaging of the human cerebral blood flow. J.Cereb.Blood Flow Metab. 1996;16;1236-49.
    'http://www.cfn.upenn.edu/perfusion/articles/perfmri_4.5.pdf

    Private InversionEfficiency As Single
    Private T1Tissue As Single
    Private BloodBrainCoeff As Single
    Private TransitTime As Single
    Private T1ArterialBlood As Single
    Private DelayTime As Single
    Private T1InRF As Single

    Private NumeratorMultiplicant As Double
    Private DenominatorMultiplicant As Double

    Public Enum ScannerTypeDefaults
        OnePointFiveTesla = 1
        ThreeTesla = 2
    End Enum

    Sub New(ByVal ScannerType As ScannerTypeDefaults, ByVal TransitTime As Single, ByVal DelayTime As Single)
        'Set up the default values for computation
        Me.InversionEfficiency = 1
        Me.BloodBrainCoeff = 0.98

        Select Case ScannerType
            Case ScannerTypeDefaults.OnePointFiveTesla
                Me.T1Tissue = 1
                Me.T1InRF = 0.75
            Case ScannerTypeDefaults.ThreeTesla
                Me.T1Tissue = 1.33
                Me.T1InRF = 0.995
        End Select

        Me.T1ArterialBlood = 1.1
        Me.TransitTime = TransitTime
        Me.DelayTime = DelayTime

        'compute Multiplicants
        Me.NumeratorMultiplicant = Me.BloodBrainCoeff / (2 * Me.InversionEfficiency * Me.T1Tissue * Math.Exp(-1 * Me.TransitTime / Me.T1ArterialBlood))
        Me.DenominatorMultiplicant = (Math.Exp((Me.TransitTime - Me.DelayTime) / Me.T1Tissue) - Math.Exp(-1 * Me.DelayTime / Me.T1Tissue) * (1 - (Me.T1InRF / Me.T1Tissue)))
        '    MsgBox(Me.NumeratorMultiplicant)
        '    MsgBox(Me.DenominatorMultiplicant)
    End Sub

    Function ReturnCBF(ByVal LabelToNonlabelRatio As Double) As Double
        Return LabelToNonlabelRatio * Me.NumeratorMultiplicant / Me.DenominatorMultiplicant
    End Function

    Function ReturnCBF(ByVal LabelToNonlabelRatio As Int16) As Int16
        Return CShort(LabelToNonlabelRatio * Me.NumeratorMultiplicant / Me.DenominatorMultiplicant)
    End Function

    Function ReturnCBF(ByVal LabelToNonlabelRatio As Single) As Single
        Return CSng(LabelToNonlabelRatio * Me.NumeratorMultiplicant / Me.DenominatorMultiplicant)
    End Function

End Class
